package cn.wikiflyer.beikang.wxapi;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.cart.OrderInfoAct;
import cn.wikiflyer.beikang.act.cart.util.ToastView;
import cn.wikiflyer.beikang.act.cart.util.ToastView.ClickListener;
import cn.wikiflyer.beikang.paywx.Constants;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelbase.BaseResp.ErrCode;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
//	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	private Context context;
	
    private IWXAPI api;
    
    private TextView tv_show;
    private TextView tv_ok;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.pay_result);
        Log.e("wj", "wxpayentry act onCreate()");
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);

        api.handleIntent(getIntent(), this);
        
        tv_show = (TextView) findViewById(R.id.textView_show);
        tv_ok = (TextView) findViewById(R.id.textView_ok);
        tv_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				WXPayEntryActivity.this.finish();
			}
		});
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		Log.e("wj", "wxpayentry act onNewIntent()");
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		Log.e("wj", "wxpayentry act onReq()");
	}
	String textShow = "";
	@SuppressLint("NewApi")
	@Override
	public void onResp(BaseResp resp) {
		Log.d("wj", "onPayFinish, errCode = " + resp.errCode);
		Log.e("orion", "errCode :"+resp.errCode+" - errStr :"+resp.errStr+" - openId :"+resp.openId+" - transaction :"+resp.transaction+" - getType :"+resp.getType());
		switch (resp.errCode) {
		case ErrCode.ERR_AUTH_DENIED://认证被否决
			textShow = "认证被否决";
			break;
		case ErrCode.ERR_COMM://一般错误
			textShow = "支付失败";
			break;
		case ErrCode.ERR_OK://正确返回
			textShow = "支付成功";
			OrderInfoAct.isWXsuccess = true;
			break;
		case ErrCode.ERR_SENT_FAILED://发送失败
			textShow = "支付失败";
			break;
		case ErrCode.ERR_UNSUPPORT://不支持错误
			textShow = "支付失败";
			break;
		case ErrCode.ERR_USER_CANCEL://用户取消
			textShow = "支付取消";
			break;
		default:
			break;
		}
//		AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		builder.setTitle("提示");
//		builder.setMessage("微信支付结果"+ resp.errStr +";code=" + String.valueOf(resp.errCode));
//		builder.setMessage(""+textShow);
//		builder.setOnDismissListener(new OnDismissListener() {
//			@Override
//			public void onDismiss(DialogInterface dialog) {
//				tv_show.setText(""+textShow);
//			}
//		});
//		builder.show();
		ToastView mToast = new ToastView.Builder(context).create(""+textShow,new ClickListener() {
			@Override
			public void clickOK() {
				WXPayEntryActivity.this.finish();
			}
		});
		mToast.show();
	}
}